import { onMounted, ref, watchEffect } from 'vue'
import { routesMap, router } from '@/router'
import { useAppStore } from '@/store/modules/app'

export const wacthRouter = (location) => {
  let currenPath = ref('/')
  let menuList = ref([])
  const appStore = useAppStore()

  onMounted(() => {
    menuList.value = routesMap.filter((item) => {
      return !item.hidden
      // return item    // TODO：感觉下面的 showClose 没用到啊，后期删了
    })
    if (appStore.breadCrumbList.length === 0) {
      const data = {
        title: router?.currentRoute?.value?.meta?.title,
        path: router?.currentRoute?.value?.path,
        showClose: false,
        type: 'route'
      }
      appStore.setBreadCrumbList(data, 'route')
    }
  })
  watchEffect(() => {
    if (location === 'menu') {
      if (router?.currentRoute.value.path && router?.currentRoute.value.path !== '/settings') {
        currenPath.value = router?.currentRoute.value.path
      }
    } else {
      if (router?.currentRoute.value.path) {
        currenPath.value = router?.currentRoute.value.path
      }
    }
  })
  return { currenPath, menuList }
}
